package com.gym.code.generator.impl.enumerate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.gym.code.generator.AbstractContentGenerator;
import com.gym.code.generator.Contants;
import com.gym.code.generator.EnumContants.FieldClassType;
import com.gym.code.generator.fmt.FreemarkerUtil;
import com.gym.code.generator.impl.degelate.method.BeanMethodGenerateHandler;
import com.gym.code.generator.impl.degelate.method.GetMethodGenerateHandlerImpl;
import com.gym.code.generator.impl.degelate.method.SetMethodGenerateHandlerImpl;
import com.gym.code.generator.model.mapping.bean.EnumGenerateModel;
import com.gym.code.generator.model.mapping.bean.EnumGenerateModel.EnumMapping;
import com.gym.code.generator.model.mapping.bean.EnumGenerateModel.EnumMappingItem;
import com.gym.code.generator.model.mapping.bean.EnumGenerateModel.EnumScope;
import com.gym.code.generator.model.mapping.bean.EnumGenerateModel.EnumValuePattern;
import com.gym.code.generator.model.mapping.field.FieldGenerateModel;

/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 * @createTime 2015-4-14 下午3:36:48
 */
public class EnumContentGenerator extends AbstractContentGenerator<EnumGenerateModel>{
	
	
	private EnumContentGenerator() {
	}

	private static EnumContentGenerator instance = new EnumContentGenerator();

	public static EnumContentGenerator getInstance() {
		return instance;
	}
	
	private BeanMethodGenerateHandler getMethod = GetMethodGenerateHandlerImpl.getInstance();
	private BeanMethodGenerateHandler setMethod = SetMethodGenerateHandlerImpl.getInstance();

	
	public  String generatePackage(EnumGenerateModel bean){
		if(EnumScope.local == bean.getScope()){
			return "";
		} else {
			return "package " + bean.getPackageName() + Contants.$lineSplit;
		}
	}
	
	@Override
	protected Set<String> doGenerateImport(EnumGenerateModel bean) {
		return Collections.emptySet();
	}

	@Override
	protected List<String> generateClassBegin(EnumGenerateModel bean) {
		return Arrays.asList("public " + (EnumScope.local == bean.getScope() ? " static ":"")  +" enum " + bean.getEnumTypeNameByFirstLetterUpper() + " {");
	}

	@Override
	protected List<String> generateFields(EnumGenerateModel bean) {
		EnumMapping enumMapping = bean.getEnumMapping();
		List<String> result = new ArrayList<String>();
		EnumMappingItem last = null;
		if(enumMapping.getEnumList().size() > 1){
			last = enumMapping.getEnumList().get(enumMapping.getEnumList().size() - 1);
		}
		for(EnumMappingItem item : enumMapping.getEnumList()) {
			StringBuilder fieldBuilder = new StringBuilder();
			fieldBuilder.append(item.getEnumName());
			fieldBuilder.append("(");
			if(EnumValuePattern.keyValue == bean.getValuePattern()){
				fieldBuilder.append(item.getValue());
				fieldBuilder.append(",");
				fieldBuilder.append("\"");
				fieldBuilder.append(item.getDesc());
				fieldBuilder.append("\"");
			}
			fieldBuilder.append(")");
			if(item != last){
				fieldBuilder.append(",");
			}
			result.add(fieldBuilder.toString());
		}
		result.add(";");
		
		result.add("private int id;");
		result.add("private String desc;");
		return result;
	}

	@Override
	protected List<String> generateMethods(EnumGenerateModel bean) {
		List<String> result = new ArrayList<String>();
		if(EnumValuePattern.keyValue == bean.getValuePattern()){
			FieldGenerateModel model = new FieldGenerateModel();
			model.setValueType(FieldClassType.numberInt);
			model.setPropertyName("id");
			result.addAll(getMethod.generateMethod(model));
			result.addAll(setMethod.generateMethod(model));
			
			model.setValueType(FieldClassType.varchar);
			model.setPropertyName("desc");
			result.addAll(getMethod.generateMethod(model));
			result.addAll(setMethod.generateMethod(model));
			
		}
		return result;
	}
	
	@Override
	protected  List<String> generateInitMethods(EnumGenerateModel bean) {
		List<String> result = new ArrayList<String>();
		if(EnumValuePattern.keyValue == bean.getValuePattern()){
			result.add(new StringBuilder().append("private ").append(bean.getEnumTypeNameByFirstLetterUpper()).append("(")
					.append("int id,String desc")
					.append(")").append("{").toString());
			result.add("this.id = id;");
			result.add("this.desc = desc;");
			result.add("}");
		}
		return result;
	}
	
	
	protected  List<String> generateOthers(EnumGenerateModel bean){
		return Arrays.asList(FreemarkerUtil.formatEnumInstanceTemplate(bean.getEnumTypeNameByFirstLetterUpper()));
	}
	
}
